package com.koron.css2.common.utils;

import com.koron.css2.common.dto.HeaderInfoDto;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class Test {
	
	public static List<Map<String, String>> importExcel(List<HeaderInfoDto> headerInfos, File file) throws Exception {
		List<String[]> data = null;   
		String[] headers = null;
		String filename = file.getName();
		String fileType = FilenameUtils.getExtension(filename);
		byte[] buf = IOUtils.toByteArray(new FileInputStream(file));
		if(Objects.equals("xlsx", fileType) || Objects.equals("xls", fileType)) {
			headers = ExcelUtils.readExcelHeader(buf, filename);
			data = ExcelUtils.readExcelData(buf, filename, true);
		}else if(Objects.equals("txt", fileType)) {
			headers = TxtUtils.readTxtHeader(buf, filename);
			data = TxtUtils.readTxtData(buf);
		}
		if(checkHeader(headerInfos, headers)) {
			return getImportExcelData(headerInfos, data, headers);
		}else {
			System.out.println("文件头不规范.....");
		}
		return null;
	}
	
	private static boolean checkHeader(List<HeaderInfoDto> headerInfos, String[] headers) {
		for(int i = 0; i < headerInfos.size(); i++) {
			boolean check = false;
			HeaderInfoDto headerInfo = headerInfos.get(i);
			for(int j = 0; j < headers.length; j++) {
				if(Objects.equals(headerInfo.getName(), headers[j])) {
					check = true;
				}
			}
			if(headerInfo.isRequired() && !check) {
				return false;
			}
		}
		return true;
	}
	
	private static List<Map<String, String>> getImportExcelData(List<HeaderInfoDto> headerInfos, List<String[]> data, String[] headers) throws IOException{
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		List<Integer> index = new ArrayList<Integer>();
		for(int i = 0; i < headerInfos.size(); i++) {
			HeaderInfoDto headerInfo = headerInfos.get(i);
			for(int j = 0; j < headers.length; j++) {
				if(Objects.equals(headerInfo.getName(), headers[j])) {
					index.add(j);
				}
			}
		}
		for(int i = 0; i < data.size(); i++) {
			String[] rowData = data.get(i);
			Map<String, String> map = new HashMap<String, String>();
			for(int j = 0; j < headerInfos.size(); j++) {
				HeaderInfoDto headerInfo = headerInfos.get(j);
				if(j < index.size()) {
					map.put(headerInfo.getCode(), rowData[index.get(j)]);
				}
			}
			result.add(map);
		}
		return result;
	}
	
	public static void main(String[] args) {
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("电话号码", true, "mobile"));
		headerInfos.add(new HeaderInfoDto("客户编号", true, "ctmNum"));
		headerInfos.add(new HeaderInfoDto("客户名称", true, "ctmName"));
		headerInfos.add(new HeaderInfoDto("客户地址", true, "ctmAddress"));
		headerInfos.add(new HeaderInfoDto("备注", false, "common"));
		headerInfos.add(new HeaderInfoDto("是是是", true, "sss"));
		try {
			File file = new File("C:\\Users\\59532\\Desktop\\ms_storage.xlsx");
			List<Map<String, String>> data = importExcel(headerInfos, file);
			System.out.println(data == null);
			System.out.println(data.size());
			data.stream().forEach(t -> {
				System.out.println(t);
			});
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}

}
